package com.atguigu.exam.service.impl;

import com.atguigu.exam.properties.MinioProperties;
import com.atguigu.exam.service.FileUploadService;
import io.minio.*;
import io.minio.errors.MinioException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.DateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * projectName: com.atguigu.exam.service.impl
 *
 * @author: 赵伟风
 * description:
 */

@EnableConfigurationProperties(value = MinioProperties.class)
@Service
@Slf4j
public class FileUploadServiceImpl implements FileUploadService {
//    @Autowired
//     private MinioClient minioClient;

    @Autowired
    private MinioProperties minioProperties;

    @Override
    public String uploadFile(MultipartFile file) {
        try {
            //1.创建客户端对象
            MinioClient minioClient = MinioClient.builder()
                    //设置Minio服务器的地址
                    .endpoint(minioProperties.getEndpoint())
                    //设置用户名和密码
                    .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                    .build();
            //检查存储桶是否已经存在
            boolean isExist =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());

            //获取文件名
            String originalFilename = file.getOriginalFilename();
            //获取文件后缀
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            //使用uuid随机生成一个字符串作为文件名
            String priffix = UUID.randomUUID().toString().replace("-", "");
            //对日期进行格式化
            String format = DateFormatUtils.format(new Date(), "yyyy/MM/dd");
            //创建文件名
            String fileName = format+"/"+priffix + suffix;
            //获取输入流
            InputStream is = file.getInputStream();
            //获取文件大小
            long size = file.getSize();


            if (isExist) {
                System.out.println("Bucket " + minioProperties.getBucketName() + " already exists.");
            } else {
                //如果桶不存在则创建它
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());
            }
            //2.创建PutLoad0bjectArgs对象
            PutObjectArgs putobjectArgs = PutObjectArgs.builder()
                    //获取minio中桶的名字
                    .bucket(minioProperties.getBucketName())
                    //获取上传文件的名称
                    .object(fileName)
                    .stream(is, size, -1)
                    .build();
//使用putObject上传文件到Minio服务器的桶中
//            minioClient.uploadObject(uploadobjectArgs);
            minioClient.putObject(putobjectArgs);
            return minioProperties.getEndpoint() + "/" + minioProperties.getBucketName() + "/" + fileName;
                 } catch (Exception e) {
                System.out.println("Error occurred:" + e);
                }
        return null;
    }


    public static void main(String[] args) {
        String fileName = "ikun.gif";
        int index = fileName.lastIndexOf(".");
        System.out.println("index = " + index);
        //获取文件名的后缀
        String suffix = fileName.substring(index);
        System.out.println("suffix = " + suffix);
        //使用uuid随机生成一个字符串作为文件名
        String  uuid = UUID.randomUUID().toString().replace("-", "");
        System.out.println("uuid = " + uuid);
    }
}

